package com.qf2.controller;

import com.qf2.domain.Account;
import com.qf2.domain.R;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

//@Controller
//@ResponseBody  //写在类的位置，相当于当前类中所有处理器方法都加上了这个注解

@RestController  //相当于@Controller+@ResponseBody
@RequestMapping("/account")
public class AccountController2 {

    //新增账户 http://localhost:80/account
//    @RequestMapping(method = RequestMethod.POST)
    @PostMapping
    public R add(@RequestBody Account account){
        int i = 1/0;
        System.out.println("新增账户:" + account);
        return R.success("新增成功");
    }

    //修改账户 http://localhost:80/account
//    @RequestMapping(method = RequestMethod.PUT)
    @PutMapping
    public R update(@RequestBody Account account){
        System.out.println("修改账户:" + account);
        return R.success("修改成功");
    }

    //查询所有账户  http://localhost:80/account
//    @RequestMapping(method = RequestMethod.GET)
    @GetMapping
    public R findAll(){
        List<Account> accounts = new ArrayList<>();
        accounts.add(new Account(1,"zs",1000d));
        accounts.add(new Account(2,"ls",2000d));
        accounts.add(new Account(3,"ww",3000d));

        return R.success(accounts);
    }

    //删除账户  http://localhost:80/account/1
//    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @DeleteMapping("/{id}")
    public R del(@PathVariable("id") Integer id){
        System.out.println("删除账户:" + id);
        return R.success("删除成功");
    }

    //查询指定id的账户  http://localhost:80/account/1
//    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @GetMapping("/{id}")
    public R findById(@PathVariable("id") Integer id){
        Account account = new Account(1, "zs", 1000d);
        return R.success(account);
    }
}
